package com.startery.template;

import java.util.Set;

import org.springframework.boot.jackson.JsonObjectSerializer;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.alibaba.fastjson.JSONObject;
import com.startery.ConnectionFactory;
import com.startery.vo.Man;

public class ZSetTest {
	
	
	
	public static void main(String[] args) {
		RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();

        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        //使用StringRedisSerializer来序列化和反序列化redis的ke
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());

        //开启事务
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.setConnectionFactory(ConnectionFactory.getInstance());
        redisTemplate.afterPropertiesSet();
        //与某一个key进行绑定
        //添加元素到变量中同时指定元素的分值。
        redisTemplate.opsForZSet().add("zSetValue",new Man("A","A1"),1);  
        redisTemplate.opsForZSet().add("zSetValue",new Man("B","B1"),3);  
        redisTemplate.opsForZSet().add("zSetValue",new Man("C","C1"),2);  
        redisTemplate.opsForZSet().add("zSetValue",new Man("D","D1"),5);  
        
        
        // 获取变量指定区间的元素。
        Set zSetValue =  redisTemplate.opsForZSet().range("zSetValue",0,-1);  
        System.out.println("通过range(K key, long start, long end)方法获取指定区间的元素:" + zSetValue);  
        
        
        
	
	}

	
}
